#导包
from common.readData import ReadData
import unittest
import requests
from ddt import ddt,data,unpack
from common.configHttp import configHttp
from common.log import logger

#1.获取测试数据
#实例化
data_obj = ReadData()
#调用实例方法
test_data = data_obj.read_excel()
print(f"===>{test_data}")
#定义一个测试类
@ddt
class TestCase(unittest.TestCase):
    #2.1创建测试用例方法
    @data(*test_data)
    @unpack
    def test_case(self,id,interfaceUrl,name,method,value,header,expect):
        #2.2获取测试数据内精选请求需要的关键字段
        logger.debug(method, interfaceUrl,value, header, expect)  #这里可以换级别  debug是最低级别
        #2.3实例化
        ch = configHttp(method,interfaceUrl,value,header)
        res = ch.run()
        res_dict = res.json()
        print(res_dict)
        #2.4断言接口返回结果,判断成功/失败
        # self.assertEqual(res_dict["errorCode"],int(expect),"测试用例不通过")


        #坑1:用if进行断言时,一定要重新抛出Assertion异常,否则默认为测试用例通过
        # if res_dict["errorCode"] == int(expect):
        #     print("测试用例通过")
        # else:
        #     print("测试用例失败")
        #     raise AssertionError("测试用例不通过~")


        #坑2:用try进行捕获异常,捕获到断言异常一定要在expect里面重新抛出断言异常,否则默认为测试用例通过
        try:
            self.assertEqual(res_dict["errorCode"],int(expect),"测试用例不通过")
        except Exception as msg:
            print("错误信息:",msg)
            raise



if __name__ == "__main__":
    unittest.main(verbosity=2)












